package com.cxy.ch1;

/**
 * 
 * @ClassName: SleepLock
 * @Description: TODO(����Sleep������Ӱ��)
 * @author ����Ѽ
 * @date 2020��4��12��
 *
 */
public class SleepLock {
	private Object lock = new Object();

	public static void main(String[] args) {
		SleepLock sleepTest = new SleepLock();
		Thread threadA = sleepTest.new ThreadSleep();
		threadA.setName("ThreadSleep");
		Thread threadB = sleepTest.new ThreadNotSleep();
		threadB.setName("ThreadNotSleep");
		threadA.start();
		try {
			Thread.sleep(1000);
			System.out.println(" Main slept!");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		threadB.start();
	}

	private class ThreadSleep extends Thread {

		@Override
		public void run() {
			String threadName = Thread.currentThread().getName();
			System.out.println(threadName + " will take the lock");
			try {

				synchronized (lock) {
					System.out.println(threadName + " taking the lock");
					Thread.sleep(5000);
					System.out.println("Finish the work: " + threadName);
				}
			} catch (InterruptedException e) {
				// e.printStackTrace();
			}
		}
	}

	private class ThreadNotSleep extends Thread {

		@Override
		public void run() {
			String threadName = Thread.currentThread().getName();
			System.out.println(threadName + " will take the lock time=" + System.currentTimeMillis());
			synchronized (lock) {
				System.out.println(threadName + " taking the lock time=" + System.currentTimeMillis());
				System.out.println("Finish the work: " + threadName);
			}
		}
	}
}
